package com.spzx.channel.service.impl;

import com.spzx.channel.service.IIndexService;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.api.RemoteCategoryService;
import com.spzx.product.api.RemoteProductService;
import com.spzx.product.api.domain.ProductSku;
import com.spzx.product.api.domain.vo.CategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class IndexServiceImpl implements IIndexService {

    @Autowired
    RemoteCategoryService remoteCategoryService;

    @Autowired
    RemoteProductService remoteProductService;

    @Override
    public Map<String, Object> getIndexData() {
        //1.获取首页一级分类集合
        R<List<CategoryVo>> categoryVoListResult = remoteCategoryService.getCategoryOne(SecurityConstants.INNER);
        if(categoryVoListResult.getCode() == R.FAIL){
            throw new ServiceException(categoryVoListResult.getMsg());
        }
        List<CategoryVo> categoryVoList = categoryVoListResult.getData(); //可能是真实结果数据，也可能是降级兜底结果数据。

        //2.获取首页Top20畅销商品集合
        R<List<ProductSku>> productSkuListResult = remoteProductService.getTopSale(SecurityConstants.INNER);
        if(R.FAIL == productSkuListResult.getCode()){
            throw new ServiceException(productSkuListResult.getMsg());
        }
        List<ProductSku> productSkuList = productSkuListResult.getData();

        //3.两个部分数据封装在Map中
        Map<String,Object> map = new HashMap<>();
        map.put("categoryList",categoryVoList);
        map.put("productSkuList",productSkuList);
        return map;
    }
}
